<?php

require_once ('html/htmlHelper.php');
require_once 'session/access.php';
require_once 'model/commonModel.php';
if (!isAuthorized("editDB.php")) {
    header("location: notAuthorized.php?page=editDB.php");
}

//a requirement has been selected
if (isset($_GET['reqId'])) {
    
}
//maintenance grid
else if (isset($_GET['grid'])) {
    getNewHtmlUpToMeta();
    getJavascriptFileLine("js/populateDropdown.js");
    getJavascriptFileLine("js/formValidation.js");
    getJavascriptFileLine("js/addRowToTable.js");
    getMantisCSSLine();
    getDatepickerCSSFiles();
    getTitle("Edit Requirements");
    getCloseTag("head");
    getOpenTag("body");
    getLogoLine();
    getLoggedInAsLine();
    getMenuBar(getUserRole());
    echo '<br><br>';
    getButtonGrid();
    echo "<div id=\"editBox\"></div>";
    echo "<div id=\"newBox\"></div>";
}
//edit requirements
else if (isset($_GET['reqEdit'])) {
    //getUpToMainMenu(getUserRole(), "Admin Main");
    $output = "";
    $output .= getNewHtmlUpToMeta();
    $output .= getJavascriptFileLine("js/populateDropdown.js");
    $output .= getJavascriptFileLine("js/formValidation.js");
    $output .= getMantisCSSLine();
    $output .= getTitle("Edit Requirements");
    $output .= getCloseTag("head");
    $output .= getOpenTag("body");
    $output .= getLogoLine();
    $output .= getLoggedInAsLine();
    $output .= getMenuBar(getUserRole());
    //the javascript that allows items to move up and down on the requirement priority form
    $output .= getReqPriorityJScript();
    if (isset($_GET['update'])) {
            $output .= CommonModel::getFlagUnorderedList($_GET['update']);
        }
    $output .= '<br><br>';    
    //get the new requirement form
    $output .= getNewRequirementFormForDb2(getAllFieldsInTable("parameter", "name"));
    $output .= '<br><br><hr><br>';
    //get the requirement priority form
    $output .= getEditRequirementPriorityForm();
    $output .= '<br><br><hr><br>';
    //get the edit requirement form
    $output .= getEditRequirementFormForDb();
    $output .= '<br><br>';    
    $output .= getCloseTag("body");
    $output .= getCloseTag("html");
    echo $output;
}
//new or edit parameter
else if (isset($_GET['paramEdit'])) {
    //getUpToMainMenu(getUserRole(), "Admin Main");
    getNewHtmlUpToMeta();
    getJavascriptFileLine("js/populateDropdown.js");
    getMantisCSSLine();
    getTitle("Edit Parameters");
    getCloseTag("head");
    getOpenTag("body");
    getLogoLine();
    getLoggedInAsLine();
    getMenuBar(getUserRole());
    //echo '<br><br>';
    //getParameterForm("edit");
    echo '<br><br>';
    getParameterMaintenanceSelector();
    echo "<div id=\"editBox\"></div>";
    echo "<div id=\"newBox\"></div>";
    getCloseTag("body");
    getCloseTag("html");
}
//new or edit sensors
else if (isset($_GET['sensorEdit'])) {
    //getUpToMainMenu(getUserRole(), "Admin Main");
    getNewHtmlUpToMeta();
    getJavascriptFileLine("js/populateDropdown.js");
    getJavascriptFileLine("js/formValidation.js");
    getMantisCSSLine();
    getTitle("Edit Sensors");
    getCloseTag("head");
    getOpenTag("body");
    getLogoLine();
    getLoggedInAsLine();
    getMenuBar(getUserRole());
    echo '<br><br>';
    getSensorMaintenanceSelector();
    echo '<br><br>';
    //if the user edits a platform related table it appears in the editPlatformBox
    echo "<div id=\"editBox\"></div>";
    echo "<div id=\"newBox\"></div>";
    echo '<br><br>';
    //getSatelliteForm("create");
    getCloseTag("body");
    getCloseTag("html");
}
//new or edit satellites
else if (isset($_GET['platformEdit'])) {
    $type = $_GET['platformEdit'];
    //getUpToMainMenu(getUserRole(), "Admin Main");
    getNewHtmlUpToMeta();
    getJavascriptFileLine("js/populateDropdown.js");
    getJavascriptFileLine("js/formValidation.js");
    getJavascriptFileLine("js/addRowToTable.js");
    getMantisCSSLine();
    getDatepickerCSSFiles();
    getTitle("Edit Satellites");
    getCloseTag("head");
    getOpenTag("body");
    getLogoLine();
    getLoggedInAsLine();
    getMenuBar(getUserRole());
    echo '<br><br>';
    //get the satellite maintenance selector
    getSatelliteMaintenanceSelector();
    echo '<br><br><hr>';
    //if the user edits a platform related table it appears in the editPlatformBox
    echo "<div id=\"editBox\"></div>";
    echo "<div id=\"newBox\"></div>";
    getCloseTag("body");
    getCloseTag("html");
}
//edit db stuff - should just be for admin
else if (isset($_GET['editDb']) && strcmp("admin", getUserRole()) === 0) {
    $output = "";
    $output .= getUpToMainMenu(getUserRole(), "Edit DB");
    $output .= '<br><br>';
    $output .= getTableNumRowsForm();
//getEditDBForm();
    $output .= '<br><br>';
    $output .= getEditTablesForm();
    $output .= getCloseTag("body");
    $output .= getCloseTag("html");
    echo $output;
} else if (strcmp("admin", getUserRole()) === 0) {
    getUpToMainMenu(getUserRole(), "Edit DB");
    echo '<br><br>';
    echo '<div class="centered">';
    echo '<table  class="width20Center" cellspacing="1">';
    echo '<caption>Select Task</caption>';
    echo ' <tr class ="fieldLabel">';
    echo '     <td class="center"><a href="editDB.php?reqEdit=1">Add/Edit Requirements</a></td>';
    echo ' </tr>';
    echo ' <tr class ="fieldLabel">';
    echo '     <td class="center"><a href="editDB.php?editDb=1">Edit Database</a></td>';
    echo ' </tr>';
    echo getCloseTag("table");
    echo getCloseTag("div");
}

echo '<br>';
echo '<br>';
if (isTestMode()) {
    if(isset($_GET['webmaster'])){             echo '<h1 class="centered">Message Successfully Sent</h1>';         }         getWebmasterFeedbackForm();
}

/**
 * the javascript that allows items to move up and down on the requirement priority form
 */
function getReqPriorityJScript() {
    return "

<script>
   function listbox_move(listID,direction){
      var listbox=document.getElementById(listID);
      var selIndex=listbox.selectedIndex;
      if(-1==selIndex){alert(\"Please select an option to move.\");return;}
      var increment=-1;
      if(direction=='up')
         increment=-1;
      else
         increment=1;
      if((selIndex+increment)<0||(selIndex+increment)>(listbox.options.length-1)){
         return;
      }
      var selValue=listbox.options[selIndex].value;
      var selText=listbox.options[selIndex].text;
      listbox.options[selIndex].value=listbox.options[selIndex+increment].value
      listbox.options[selIndex].text=listbox.options[selIndex+increment].text
      listbox.options[selIndex+increment].value=selValue;
      listbox.options[selIndex+increment].text=selText;
      listbox.selectedIndex=selIndex+increment;
   }
   function ColectSel(selid,tbid){
      var sel=document.getElementById(selid);
      var tb=document.getElementById(tbid);
      tb.value='';
      for (var zxc0=0;zxc0<sel.options.length;zxc0++){
         tb.value+=sel.options[zxc0].value+',';
      }
   }
</script>";
}

?>
